<template>
    <div class="goodsinfo-content">
        <transition
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter">
            <div class="ball" v-show="ballflag" ref="ball"></div>
        </transition>
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper-box :lunbotuList="lunbotu" :isfull="false" :max="msglist.stock_quantity"></swiper-box>
                </div>
            </div>
		</div>
        <div class="mui-card">
            <div class="mui-card-header">{{msglist.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:<del>¥{{msglist.market_price}}</del>&nbsp;&nbsp; 销售价:<span>¥{{msglist.sell_price}}</span>
                    </p>
                    <p>购买数量:<number-box :id="id" @getcount="getSelectCount" :max="msglist.stock_quantity"></number-box></p>
                    <p>
                        <mt-button type="primary" size="small" >立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="toshopcar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{msglist.goods_no}}</p>
                    <p>库存情况：{{msglist.stock_quantity}}件</p>
                    <p>上架时间：{{msglist.add_time | dateFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain  @click="goodsdesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain  @click="goodscomment(id)">商品评论</mt-button></div>
            </div>
    </div>
</template>

<script>
import swiper from "../subcomponents/swiper.vue"
import numberbox from "../subcomponents/numberbox.vue"
export default {
    data() {
        return{
            id:this.$route.params.id,
            lunbotu: [],
            msglist: {},
            ballflag: false,
            selectcount: 1,
        }
    },
    created(){
        this. getlunbotu();
        this. getgoodsmsg();
    },
    methods:{
        getlunbotu(){
            this.$http.get('api/getthumimages/'+this.id).then(result=>{
                if(result.body.status === 0){
                    result.body.message.forEach(item=>{
                        item.img_url = item.src;
                    })
                    this.lunbotu = result.body.message;
                }
            })
        },
        getgoodsmsg(){
            this.$http.get("api/goods/getinfo/"+this.id).then(result=>{
                if(result.body.status === 0 ){
                    this.msglist = result.body.message[0];
                }
            })
        },
        goodsdesc(id){
            // 编程式跳转页面
            this.$router.push({name:"goodsdesc",params:{id}});  
        },
        goodscomment(id){
            this.$router.push({name:"goodscomment",params:{id}});
        },
        toshopcar(){
            this.ballflag = ! this.ballflag;
            // 点击加入购物车按钮，拼接出一个goodsinfo对象出来,然后将数据保存在vuex的stata中的car中
            var goodsinfor = {
                id:this.id,
                count: this.selectcount,
                price: this.msglist.sell_price,
                selected: true,
            };
            this.$store.commit('addtocar', goodsinfor);
        },
        beforeEnter(el){
            el.style.transform = "translate(0,0)"
        },
        enter(el,done){
            el.offsetWidth;
            // 动态获取小球和购物车小标的横纵坐标差，确定动画中小球移动的距离
            const ballPosition = this.$refs.ball.getBoundingClientRect();
            const badgePosition = document.getElementById("badge").getBoundingClientRect();

            const XDist = badgePosition.left - ballPosition.left;
            const YDist = badgePosition.top - ballPosition.top;

            el.style.transform = `translate(${ XDist}px,${YDist}px)`;
            el.style.transition = "all 0.5s cubic-bezier(.4, -0.3, 1, .68)";
            done();
        },
        afterEnter(el){
            this.ballflag = !this.ballflag;
        },
        getSelectCount(count){
            this.selectcount = count;
            // console.log("父组件拿到的值:" + count)
        }

    },
    components:{
        "swiper-box": swiper,
        "number-box": numberbox,
    }
}
</script>

<style lang="scss" scoped>
.goodsinfo-content{
    background-color: #ccc;
    overflow: hidden;
    .mui-card-content-inner{
        number-box{
            display: inline-block;
        }
    }

        .price{
            span{
                color: red;
            }
        }
        
    .mui-card-footer{
        display: block;
        button{
            margin: 10px 0;
        }
    }
    .ball{
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background-color: rgb(240, 9, 9);
        z-index: 999;
        position: absolute;
        top: 385px;
        left: 140px;


    }
}
</style>